package com.timepies.view;

import static com.timepies.database.DbManager.getDB;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.timepies.R;
import com.timepies.activity.DettaglioPieActivity;
import com.timepies.database.UiManager;
import com.timepies.domain.Timepie;
import com.timepies.domain.TimepieAdapter;
import com.timepies.domain.TimepieAdapterDatabase;
import com.timepies.domain.enums.MenuPies;
import com.timepies.util.Easter.YearOutOfRangeException;
import com.timepies.util.Festivita;

public class ElencoPieList implements OnItemClickListener {

	private Activity activity;
	private ListView list;

	public ElencoPieList(Activity activity) {

		this.activity = activity;
		this.list = (ListView) activity.findViewById(R.id.pie_main_list);
		this.list.setOnItemClickListener(this);

		MenuPies menu = UiManager.getUiManager().getMenuPies();
		load(menu.getPosizione());
	}

	public void load(int itemPosition) {
		Log.d(this.toString(), "load p:" + itemPosition);

		TextView titolo = (TextView) activity.findViewById(R.id.mainlist_title);
		String nuovoTitolo = "";
		MenuPies menu = MenuPies.valueOf(itemPosition);

		try {
			String[] tot = Festivita.countFesteAndPonti().split(",");
			switch (menu) {

			case FESTIVITA:
				nuovoTitolo = String.format(menu.getTitolo(), tot[0], tot[1],
						tot[1]);
				list.setAdapter(new TimepieAdapter(activity, R.id.listView,
						Festivita.calcolaProssimeFeste()));
				titolo.setText(nuovoTitolo);
				break;

			case PONTI:
				int sum = Integer.parseInt(tot[1]) + Integer.parseInt(tot[3]);
				nuovoTitolo = String.format(MenuPies.PONTI.getTitolo(), tot[0],
						tot[1], tot[1], tot[2], sum, tot[3]);
				list.setAdapter(new TimepieAdapter(activity, R.id.listView,
						Festivita.calcolaProssimiPonti()));
				titolo.setText(nuovoTitolo);
				break;

			case TIMEPIES:
				Cursor cursor = getDB(activity).cursor();
				list.setAdapter(new TimepieAdapterDatabase(activity, cursor, 0));
				titolo.setText(activity.getText(R.string.mainlist_title));
				break;

			}

			UiManager.getUiManager().setMenuPies(menu);
			activity.getActionBar().setTitle(menu.getDescrizione());

		} catch (YearOutOfRangeException e) {
			e.printStackTrace();
		}

		// istruzioni
		boolean vuota = list.getCount() == 0;
		TextView istruzioni = (TextView) activity.findViewById(R.id.istruzioni);
		istruzioni.setVisibility((vuota) ? View.VISIBLE : View.GONE);

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

		Log.d(this.toString(), "p:" + position + " id:" + id);

		String data = ((TextView) v.findViewById(R.id.txt_date)).getText()
				.toString();

		String testo = ((TextView) v.findViewById(R.id.txt_testo)).getText()
				.toString();

		String oggetto = ((TextView) v.findViewById(R.id.txt_oggetto))
				.getText().toString();

		Intent i = new Intent(activity, DettaglioPieActivity.class);
		i.putExtra("pieObj", new Timepie(id, data, oggetto, testo));
		activity.startActivity(i);
		activity.finish();

		Log.d(this.toString(), "p:" + position + " id:" + id + " SENDED!");

	}
}
